
package robotinterface;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import robotinterface.client.RobotClient;

/**
 *
 * Nate Hemmes
 */
public class ManualTab extends JComponent{

    //static public RobotClient robotClient;
    ArrayList<BuildManual> panels;

    static final int min = 0;
    static final int max = 100;
    static final int init = 50;

    public ManualTab() {


        panels = new ArrayList<BuildManual>();

        SpringLayout manualLayout = new SpringLayout();
        setLayout(manualLayout);

        setBackground(Color.DARK_GRAY);
        setForeground(Color.DARK_GRAY);

        JLabel valLabel = new JLabel("Value");
        add(valLabel);
        manualLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, valLabel, 0, SpringLayout.HORIZONTAL_CENTER, this);
        manualLayout.putConstraint(SpringLayout.NORTH, valLabel, 10, SpringLayout.NORTH, this);


        BuildManual joint1Panel = new BuildManual(this, "Joint 1:", -150, 150, "-150", "150");
        add(joint1Panel); panels.add( joint1Panel );
        manualLayout.putConstraint(SpringLayout.NORTH, joint1Panel, 10, SpringLayout.SOUTH, valLabel);
        manualLayout.putConstraint(SpringLayout.WEST, joint1Panel, 10, SpringLayout.WEST, this);
        manualLayout.putConstraint(SpringLayout.SOUTH, joint1Panel, 64, SpringLayout.SOUTH, valLabel);
        manualLayout.putConstraint(SpringLayout.EAST, joint1Panel, -10, SpringLayout.EAST, this);
        joint1Panel.setTooltip( "Joint 1", "images/joint0.jpg"); /* Add Tooltip with Image */


        BuildManual joint2Panel = new BuildManual(this,"Joint 2:", -95, 95, "-95", "95");
        add(joint2Panel); panels.add( joint2Panel );
        manualLayout.putConstraint(SpringLayout.NORTH, joint2Panel, 7, SpringLayout.SOUTH, joint1Panel);
        manualLayout.putConstraint(SpringLayout.WEST, joint2Panel, 10, SpringLayout.WEST, this);
        manualLayout.putConstraint(SpringLayout.SOUTH, joint2Panel, 61, SpringLayout.SOUTH, joint1Panel);
        manualLayout.putConstraint(SpringLayout.EAST, joint2Panel, -10, SpringLayout.EAST, this);
        joint2Panel.setTooltip( "Joint 2", "images/joint1.jpg"); /* Add Tooltip with Image */


        BuildManual joint3Panel = new BuildManual(this,"Joint 3:", -150, 150, "-150", "150");
        add(joint3Panel); panels.add( joint3Panel );
        manualLayout.putConstraint(SpringLayout.NORTH, joint3Panel, 7, SpringLayout.SOUTH, joint2Panel);
        manualLayout.putConstraint(SpringLayout.WEST, joint3Panel, 10, SpringLayout.WEST, this);
        manualLayout.putConstraint(SpringLayout.SOUTH, joint3Panel, 61, SpringLayout.SOUTH, joint2Panel);
        manualLayout.putConstraint(SpringLayout.EAST, joint3Panel, -10, SpringLayout.EAST, this);
        joint3Panel.setTooltip( "Joint 3", "images/joint2.jpg"); /* Add Tooltip with Image */


        BuildManual joint4Panel = new BuildManual(this,"Joint 4:", -95, 95, "-95", "95");
        add(joint4Panel); panels.add( joint4Panel );
        manualLayout.putConstraint(SpringLayout.NORTH, joint4Panel, 7, SpringLayout.SOUTH, joint3Panel);
        manualLayout.putConstraint(SpringLayout.WEST, joint4Panel, 10, SpringLayout.WEST, this);
        manualLayout.putConstraint(SpringLayout.SOUTH, joint4Panel, 61, SpringLayout.SOUTH, joint3Panel);
        manualLayout.putConstraint(SpringLayout.EAST, joint4Panel, -10, SpringLayout.EAST, this);
        joint4Panel.setTooltip( "Joint 4", "images/joint3.jpg"); /* Add Tooltip with Image */


        BuildManual joint5Panel = new BuildManual(this,"Joint 5:", -150 , 150, "-150 ", "150");
        add(joint5Panel); panels.add( joint5Panel );
        manualLayout.putConstraint(SpringLayout.NORTH, joint5Panel, 7, SpringLayout.SOUTH, joint4Panel);
        manualLayout.putConstraint(SpringLayout.WEST, joint5Panel, 10, SpringLayout.WEST, this);
        manualLayout.putConstraint(SpringLayout.SOUTH, joint5Panel, 61, SpringLayout.SOUTH, joint4Panel);
        manualLayout.putConstraint(SpringLayout.EAST, joint5Panel, -10, SpringLayout.EAST, this);
        joint5Panel.setTooltip( "Joint 5", "images/joint4.jpg"); /* Add Tooltip with Image */


        BuildManual joint6Panel = new BuildManual(this,"Joint 6:", -95, 95, "-95", "95");
        add(joint6Panel); panels.add( joint6Panel );
        manualLayout.putConstraint(SpringLayout.NORTH, joint6Panel, 7, SpringLayout.SOUTH, joint5Panel);
        manualLayout.putConstraint(SpringLayout.WEST, joint6Panel, 10, SpringLayout.WEST, this);
        manualLayout.putConstraint(SpringLayout.SOUTH, joint6Panel, 61, SpringLayout.SOUTH, joint5Panel);
        manualLayout.putConstraint(SpringLayout.EAST, joint6Panel, -10, SpringLayout.EAST, this);
        joint6Panel.setTooltip( "Joint 6", "images/joint5.jpg"); /* Add Tooltip with Image */


        BuildManual joint7Panel = new BuildManual(this,"Joint 7:", -95, 95, "-95", "95");
        add(joint7Panel); panels.add( joint7Panel );
        manualLayout.putConstraint(SpringLayout.NORTH, joint7Panel, 7, SpringLayout.SOUTH, joint6Panel);
        manualLayout.putConstraint(SpringLayout.WEST, joint7Panel, 10, SpringLayout.WEST, this);
        manualLayout.putConstraint(SpringLayout.SOUTH, joint7Panel, 61, SpringLayout.SOUTH, joint6Panel);
        manualLayout.putConstraint(SpringLayout.EAST, joint7Panel, -10, SpringLayout.EAST, this);
        joint7Panel.setTooltip( "Joint 7", "images/joint6.jpg"); /* Add Tooltip with Image */


        //BuildManual gripperPanel = new BuildManual(this,"Gripper:", -800, 800, "-0.008", "    0.008", -0.008, 0.008);//carful, since being added as 8 and not 0008
        BuildManual gripperPanel = new BuildManual(this,"Gripper:", 0, 100, "0", "    100");
        add(gripperPanel); panels.add( gripperPanel );
        manualLayout.putConstraint(SpringLayout.NORTH, gripperPanel, 7, SpringLayout.SOUTH, joint7Panel);
        manualLayout.putConstraint(SpringLayout.WEST, gripperPanel, 10, SpringLayout.WEST, this);
        manualLayout.putConstraint(SpringLayout.SOUTH, gripperPanel, 61, SpringLayout.SOUTH, joint7Panel);
        manualLayout.putConstraint(SpringLayout.EAST, gripperPanel, -10, SpringLayout.EAST, this);
        gripperPanel.setTooltip( "Gripper", "images/joint7.jpg"); /* Add Tooltip with Image */

    }

    // Really gross code for sending data, Should have been implemented from start.
    public void sendMessage()
    {
        new Thread( new Runnable() {
            @Override
            public void run()
            {
                double[] joints = new double[ panels.size() ];
                for (int x = 0; x < panels.size(); x++)
                    joints[x] = panels.get( x ).getValue();

                String recvMessage = RobotClient.sendMessage( joints );
                //System.out.printf( "Received from Server:\n  %s\n", recvMessage );
            }
        }).start();
    }

}
